<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#if includeSettings>
	<#assign settingsName=config.settings.@name[0]!"Settings">
	<@pp.changeOutputFile name="${javaDir}${packageDir}/service/support/${settingsName}Service.java" />
	

package ${project.@rootPackage}.service.support;

import ${project.@rootPackage}.service.util.SettingsUtil;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Controller class for ${settingsName}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
@Service
public class ${settingsName}Service {

	private static final Logger logger = Logger.getLogger("${settingsName}Service");
    protected String path;
    protected String SETTINGS_FILE = "settings.properties";
    <#list config.settings.setting as setting>
    private final String ${setting.@name?upper_case}_DEFAULT="${setting.@default}";
    </#list>

    @PostConstruct
    public void init() {
        try {
            Resource resource = new ClassPathResource(SETTINGS_FILE);
            path = resource.getFile().getPath();
        } catch (Exception e) {
            System.out.println(e);
        }
        
        Properties settings = SettingsUtil.getSettings(path);
        if (settings == null || settings.isEmpty()) {
            if (!setDefaultValues()) {
                System.out.println("Error while saving default settings in init method");
            }
        }
    }

    private boolean setDefaultValues() {
        System.out.println("Create preferencias");
        Properties settings = new Properties();
        <#list config.settings.setting as setting>
    	settings.setProperty("${setting.@name}",${setting.@name?upper_case}_DEFAULT);
    	</#list>
        return SettingsUtil.saveSettings(settings, path);
    }

    public boolean restoreDefaultValues() {
        return setDefaultValues();
    }

    public boolean saveSettings(Properties settings) {
        return SettingsUtil.saveSettings(settings, path);
    }

    public Properties getSettings() {
        return SettingsUtil.getSettings(path);
    }
}
</#if>
